<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<style type="text/css">
#main {
    margin: 0 auto;
    border: 1px green solid;
    width: 800px;
}

.row {
    border-bottom: 1px red dotted;
    padding: 10px 0px;
}

.lspan {
    font-weight: bold;
    width: 150px;
    display: inline-block;
    margin-left: 10px;
}
</style>
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
var appJson = "application/json";
var appForm = "application/x-www-form-urlencoded";

function loginHandler() {
	//
	// loginName, password, email
	// requried to validate again when clicking on the register button
	
	var loginName = $('#loginName').val();
	var password = $('#password').val();

	if((loginName == null || loginName == '')) {
        alert("LoginName can not be empty!")
        return;
    }
	
	if((password == null || password == '')) {
        alert("Password can not be empty!")
        return;
    }
	
	var userinfo = {
            "loginName" : loginName,
            "userPassword" : password,
            "remember" : false
    };
    
    $.ajax({
        url: '/login',
        type: 'POST',
        contentType: appJson,
        dataType: 'json',
        data: JSON.stringify(userinfo),
        beforeSend : function(req) {

        },
         success:function(data){
        	 var mymsg = data.data.token ? data.data.token : data.data.msg;
        	 localStorage.setItem("token", mymsg);
        	 $('#msgs').html('<span style="color:green">'+ 'Login Successfully.'+ '</span>')
         },
         error:function(data){
                alert("Fail");
         }
    });
}

</script>
</head>
<body>
<div id="main">
<h1 align="center">Login</h1>
<form action="/user/login">
<div id="messages" class="msg">
    <div id="msgs" style="margin-left: 10px">
     
    </div>
    
</div>
<div id="row" class="row">
    <span id="lspan" class="lspan">
        Login Name: 
    </span>
    <span id="cspan">
        <input type="text" name="loginName"  size="30" id="loginName" placeholder="User Name or Email"/>
    </span>
</div>

<div id="row1" class="row">
    <span id="lspan" class="lspan">
        Password: 
    </span>
    <span id="cspan">
        <input type="password" name="password" id="password"/>
    </span>
</div>

<div id="row3" class="row">
    <span style="margin-left: 10px">
     <button onclick="loginHandler();return false;">Login</button>
    </span>
    
</div>


</form>
</div>
</body>
</html>